<?php

/**
 * GalleryController
 * 
 * @author
 * @version 
 */

require_once 'Zend/Controller/Action.php';

class Cms_GalleryController extends Zend_Controller_Action {
	/**
	 * The default action - show the home page
	 */
	public function indexAction() {
		CmsAuth::check();
		
		if (NULL === $id = $this->getRequest()->getParam('id')) {
			throw new Zend_Controller_Action_Exception('Required param "Id"', 404);
		}
		
		$galleryItem = MF_Main::getInstance()->Gallery->getById($id);
		
		$this->view->gallery_id = $galleryItem->_content_id;
		$this->view->image_types = $galleryItem->image_types;
		
		$gallery_cfg = $galleryItem->config;
		$data_map = array();
		if ($gallery_cfg!==false) {
			for ($g=0;$g<count($gallery_cfg);$g++) {
				array_push($data_map,$gallery_cfg[$g][3]);
			}
		}
		$this->view->data_map = $data_map;
	}

	
	public function dataAction() {
		CmsAuth::check();
		
		if (NULL === $id = $this->getRequest()->getParam('id')) {
			throw new Zend_Controller_Action_Exception('Required param "Id"', 404);
		}
		
		$galleryItem = MF_Main::getInstance()->Gallery->getById($id);
		
		
		if (NULL !== $act = $this->getRequest()->getParam('act')) {
			switch ($act) {
				case 'add':
					$galleryItem->add();
					break;
				
				case 'del':
					if (NULL === $image_id = $this->getRequest()->getParam('image_id')) {
						throw new Zend_Controller_Action_Exception('Required param "image_id" while del action precessed', 404);
					}
					$galleryItem->getById($image_id)->delete();
					break;
					
				default:
					;
				break;
			}
			
		}
		
		
		$data = array();
		for ($i=0;$i<$galleryItem->length();$i++) {
			$gallery_image_data = array(
					array('image_name',
						array('id' => $galleryItem->image($i)->_content_id, 
							'value' => $galleryItem->image($i)->name)),
				);
				
			for ($t=0;$t<count($galleryItem->image_types);$t++) {
				$key_name = $galleryItem->image_types[$t][0];
				array_push($gallery_image_data,
					$this->_helper->WidgetAbstract($galleryItem->image($i)->$key_name));
			}
			if ($galleryItem->config!==false) {
				for ($c=0;$c<count($galleryItem->config);$c++) {
					$key_name = $galleryItem->config[$c][0]; 
					array_push($gallery_image_data,$this->_helper->WidgetAbstract($galleryItem->image($i)->$key_name));
				}
			}
			
			array_push($gallery_image_data,
				/*array('image_up',array('id' => $galleryItem->image($i)->_content_id)),
				array('image_down',array('id' => $galleryItem->image($i)->_content_id)),*/
				array('image_delete',array('id' => $galleryItem->image($i)->_content_id))
			);
			
			
			array_push($data,$gallery_image_data);
		}
		
		$this->view->data = $data;
		$this->renderScript('json.phtml');
	}
	
	
	public function changeAction() {
		CmsAuth::check();
		
		if (NULL === $id = $this->getRequest()->getParam('id')) {
			throw new Zend_Controller_Action_Exception('Required param "Id"', 404);
		}
		if (NULL === $gallery_id = $this->getRequest()->getParam('gallery_id')) {
			throw new Zend_Controller_Action_Exception('Required param "gallery_id"', 404);
		}
		
		$galleryImage = MF_Main::getInstance()->Gallery->getById($gallery_id)->getById($id);
		
		if (NULL !== $title = $this->getRequest()->getParam('name')) {
			$title = $title; 
			$galleryImage->name = $title;
		}
		
	
		
		$this->view->result = '1';
		$this->_helper->layout->disableLayout(); 
	}
	
	public function filledAction() {
		CmsAuth::check();
		
		if (NULL === $id = $this->getRequest()->getParam('id')) {
			throw new Zend_Controller_Action_Exception('Required param "Id"', 404);
		}
		
		$this->view->result = MF_Main::getInstance()->Gallery->getById($id)->length()==0 ? '0' : '1'; 
		$this->_helper->layout->disableLayout();
	}
	
}
